/*
 * @Author: xiaosihan
 * @Date: 2024-06-22 16:33:16
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-01 19:46:13
 */

// 零件详情页的公共状态

import { TreeDataNode } from "antd";
import { observable, configure } from "mobx";
configure({ enforceActions: "never" });
import { nanoid } from "nanoid";
import partLableApi from "@api/partCollaborateApi";
import { get } from "lodash";
import { PartLableData } from "@type";
import { triggerAsyncId } from "async_hooks";

export type ModelLyaer = {
  name: string;
  children: Array<ModelLyaer>;
};

const partCollaborateStore = (window.partCollaborateStore = observable(
  {
    // 上一页地址
    formPath: "",
    formName: "",
    setFormPath(formPath: string, formName: string) {
      this.formPath = formPath;
      this.formName = formName;
    },
    // 模型id
    modelId: "",
    setModelId(modelId: string = "") {
      this.modelId = modelId;
    },

    //零件属于哪个用户的用户id
    userId: "",
    setUserId(userId: string) {
      this.userId = userId;
    },

    // 保存每个mesh 爆炸的方向
    blastDirection: [] as Array<{ x: number; y: number; z: number }>,
    setBlastDirection(
      blastDirection: Array<{ x: number; y: number; z: number }>
    ) {
      this.blastDirection = blastDirection;
    },

    //爆炸的进度
    blastProgress: 0,
    setBlastProgress(blastProgress: number) {
      this.blastProgress = blastProgress;
    },

    //模型的尺寸
    modalSize: { x: 0, y: 0, z: 0 },
    setModalSize(modalSize: { x: number; y: number; z: number }) {
      this.modalSize = modalSize;
    },

    //加载中
    loadding: false,
    setLoadding(loadding: boolean) {
      this.loadding = loadding;
    },

    //模型的名称
    modelName: "",
    setModelName(modelName: string) {
      this.modelName = modelName;
    },

    //模型的大小
    size: "",
    setSize(size: string) {
      this.size = size;
    },

    //模型的日期
    createTime: "",
    setCreateTime(createTime: string) {
      this.createTime = createTime;
    },

    //模型结构
    modelLyaer: [] as Array<TreeDataNode & { isMesh: boolean }>,
    setModelLyaer(modelLyaer: Array<TreeDataNode & { isMesh: boolean }>) {
      this.modelLyaer = modelLyaer;
    },

    //当前选中的部位的id
    activeId: [] as Array<string>,
    setActiveId(activeId: Array<string> = []) {
      this.activeId = activeId;
    },

    //点选模型
    selectModel: false,
    setSelectModel(selectModel: boolean) {
      this.selectModel = selectModel;
      this.addMark = false;
    },

    //需要隐藏的对象的id
    hideIds:[] as Array<string>,
    addHideId(id: string){
        if(id){
            this.hideIds = [...this.hideIds,id];
        }
    },
    removeHideId(id: string){
        if(id){
            this.hideIds = this.hideIds.filter((item)=>item !== id);
        }
    },
    triggerHideId(id: string){
        if(this.hideIds.includes(id)){
            this.removeHideId(id);
        }else{
            this.addHideId(id)
        }
    },
    setHideId(hideIds: Array<string>){
        this.hideIds = hideIds;
    },

    //添加标记
    addMark: false,
    setAddMark(addMark: boolean) {
      this.addMark = addMark;
      this.selectModel = false;
    },

    // 控制器的类型
    controlType: "OrbitControls" as "MapControls" | "OrbitControls",
    setControlType(controlType: "MapControls" | "OrbitControls") {
      this.controlType = controlType;
    },

    //标签数据
    partLableDatas: [] as Array<PartLableData>,
    setPartLableData(partLableDatas: Array<PartLableData>) {
      this.partLableDatas = partLableDatas;
    },
    async getPartLableData() {
      const result = await partLableApi.get(1, 100, this.modelId);
      const partLableDatas = get(result, [
        "data",
        "data",
      ]) as Array<PartLableData>;
      this.setPartLableData(partLableDatas);
    },

    //选中的评论
    selectLableId: "",
    setSelectLableId(selectLableId: string) {
      if (this.selectLableId === selectLableId) {
        this.selectLableId = "";
      } else {
        this.selectLableId = selectLableId;
      }
    },

    //隐藏标点
    hideLable: false,
    setHideLable(hideLable: boolean) {
      this.hideLable = hideLable;
    },

    //显示右键菜单
    showRightMenu: false,
    setShowRightMenu(showRightMenu: boolean) {
      this.showRightMenu = showRightMenu;
    },
    //右键菜单的位置
    rightMenuPosition: { x: 0, y: 0 },
    setRightMenuPosition(rightMenuPosition: { x: number; y: number }) {
      this.rightMenuPosition = rightMenuPosition;
    },
    //单独显示对象
    showSingleObject: false,
    setShowSingleObject(showSingleObject: boolean) {
      this.showSingleObject = showSingleObject;
    },
    //透明显示
    transparent: false,
    setTransparent(transparent: boolean) {
      this.transparent = transparent;
    },
    
    //恢复所有
    restoreAll(){
        this.showSingleObject = false;
        this.activeId = [];
        this.hideIds = [];
        this.hideLable = false;
        this.showRightMenu = false;
        this.transparent = false;
    }
  },
  {},
  { deep: false }
));

export default partCollaborateStore;
